﻿using SHKF.B2C.Mannage.AdminUI.Model.ProductBase;
using SHKF.B2C.Mannage.AdminUI.Network;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Dto.Center.ProductBase;
using SHKF.B2C.Model.Entity.Center;
using SHKF.B2C.Model.Req;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component;

namespace SHKF.B2C.Mannage.AdminUI.View.CenterInfo.ProductBase
{
    /// <summary>
    /// frmProductBase.xaml 的交互逻辑
    /// </summary>
    public partial class frmProductBase : BasePage
    {
        private ProductBaseViewModel data;
       
        public frmProductBase()
        {
            InitializeComponent();
        }


        private void BasePage_Loaded(object sender, RoutedEventArgs e)
        { 
            this.data = new ProductBaseViewModel();
            this.DataContext = this.data;
        }

        private void btnProductAdd_Click(object sender, RoutedEventArgs e)
        {
            base.Contain.Navigate("/View/CenterInfo/ProductBase/frmProductBaseEdit", new ProductBaseEntity());
        }

        private void btnProductEdit_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgv.SelectedItems.Count > 0)
            {
                ProductBaseEntity info = (ProductBaseEntity)this.dgv.SelectedValue;
                base.Contain.Navigate("/View/CenterInfo/ProductBase/frmProductBaseEdit", info.Clone());
            }
        }

        private async void btnProductDelete_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgv.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }

            if ( MessageBox.Show("请确认是否要删除?","删除提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK )
            { 
                List<long> list = new List<long>();
                foreach (ProductBaseEntity info in this.dgv.SelectedItems)
                {
                    list.Add(info.SN);
                }

                DataDto dto = await GL.HttpClientManage.ProductBaseDelete(list);
                if (dto.error == null)
                {
                    MessageBox.Show("删除成功!");
                    this.LoadProduct();
                }
                else
                {
                    MessageBox.Show(dto.error.msg);
                }
            }
           
        }
 
        private void dgv_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            int PageSize = int.Parse(this.cbxPageSize.Text);
            int PageIndex = int.Parse(this.txtPageIndex.Text);
            e.Row.Header = e.Row.GetIndex() + PageSize*(PageIndex-1) + 1;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            LoadProduct();
        }

        public override void CallBack(object model)
        {
            base.CallBack(model);

            LoadProduct();
        }

        private async void LoadProduct()
        {
            string upc = this.data.Upc;
            string ProductName = this.data.ProductName;
            string CommonTitle = this.data.CommonTitle;
            string LiceseId = this.data.LiceseId;

            try
            {
                ProductBasePageReq req = new ProductBasePageReq();
                req.Upc = upc;
                req.ProductName = ProductName;
                req.CommonTitle = CommonTitle;
                req.LiceseId = LiceseId;
                req.PageSize = int.Parse( this.cbxPageSize.Text );
                req.PageIndex = int.Parse(this.txtPageIndex.Text);

                ProductBasePageDto dto = await GL.HttpClientManage.ProductBaseQuery(req);
                if (dto.error == null)
                {
                    this.data.Data = dto.data;
                    this.txtPageCount.Text = dto.Page.PageCount.ToString();
                    this.txtDataCount.Text = dto.Page.Total.ToString();
                }
                else
                {
                    MessageBox.Show(dto.error.msg);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnReduce_Click(object sender, RoutedEventArgs e)
        {
            int index = int.Parse(txtPageIndex.Text);
            if(index > 1)
            {
                index--;
                txtPageIndex.Text = $"{index}";
                LoadProduct();
            }

            
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            int pageCount = int.Parse(txtPageCount.Text);
            int index = int.Parse(txtPageIndex.Text);
            if (index < pageCount)
            {
                index++;
                txtPageIndex.Text = $"{index}";
                LoadProduct();
            }

           
        }
    }
}
